package com.n9.qqonline.sales.product;

import com.n9.excel.ExcelUtils;
import com.n9.qqonline.sales.entity.Config;
import com.n9.util.DateUtils;
import com.n9.util.StringUtils;

import java.io.File;
import java.io.FileInputStream;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author ice
 * @since 2019/5/30 11:05
 */
public class ExcelFileLoader implements PatentLoader {

    private String filePath;

    public ExcelFileLoader(String filePath) {
        this.filePath = filePath;
    }

    @Override
    public void load(MsgDisplayer displayer) {
        if (!StringUtils.hasText(filePath)) {
            displayer.display("专利清单文件为空,无法设置");
            return;
        }

        File file = new File(filePath);
        if (!file.exists()) {
            displayer.display(String.format("文件不存在:%s", filePath));
            return;
        }
        try {
            List<Patent> patents = ExcelUtils.readRows(new FileInputStream(file), (rowDatas)->{
                if (rowDatas.length < 7) {
                    return null;
                }
                String tags = Objects.isNull(rowDatas[1]) ? "" : rowDatas[1].toString();
                String no = Objects.isNull(rowDatas[2]) ? "" : rowDatas[2].toString();
                String name = Objects.isNull(rowDatas[3]) ? "" : rowDatas[3].toString();
                String status = Objects.isNull(rowDatas[4]) ? "" : rowDatas[4].toString();
                String expireDay = "";
                Object date = rowDatas[5];
                if (date instanceof String) {
                    expireDay = Objects.isNull(date) ? "" : date.toString();
                } else if (date instanceof Date){
                    if (Objects.nonNull(date)) {
                        expireDay = DateUtils.format((Date) date, "yyyy-MM-dd");
                    }
                }
                String saleStatus = Objects.isNull(rowDatas[6]) ? "" : rowDatas[6].toString();
                String notes = Objects.isNull(rowDatas[7]) ? "" : rowDatas[7].toString();

                Patent patent = new Patent();
                patent.setTags(tags);
                patent.setNo(no);
                patent.setName(name);
                patent.setStatus(status);
                patent.setExpireDay(expireDay);
                patent.setSaleStatus(saleStatus);
                patent.setNotes(notes);
                return patent;
            }, 3);

            PatentService ps = new MemoryPatentService();
            patents.forEach(patent -> {
                String[] split = patent.getTags().split(Config.SEPARATOR_REGEX);
                for (int i = 0; i < split.length; i++) {
                    ps.addPatent(split[i], patent);
                }
            });
            displayer.display(filePath);
            displayer.display(String.format("专利清单文件配置成功,专利数:%d,标签数:%d", patents.size(), ps.getTags().size()));
        } catch (Exception e) {
            e.printStackTrace();
            displayer.display("专利清单设置错误");
        }
    }
}
